package com.hhit.bigscreenbackmanage.service.impl.jjgl;

import cn.hutool.core.util.IdUtil;
import com.hhit.bigscreenbackmanage.dao.jjgl.JjglImportMapper;
import com.hhit.bigscreenbackmanage.entity.jdgl.Ndjh;
import com.hhit.bigscreenbackmanage.entity.jjgl.*;
import com.hhit.bigscreenbackmanage.util.Utils;
import com.hhit.bigscreenbackmanage.vo.jjgl.NdjhVO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class JjglImportServiceImpl {


    @Resource
    JjglImportMapper jjglImportMapper;

    /**
     * 导入 -------- 施工图预算附表报
     * @param
     */
    public Map<String,Object> importSgtYsglssqkybData(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglSgtYsglssqkyb> list = (List<TJjglSgtYsglssqkyb>)Utils.mapToEntiry(result, TJjglSgtYsglssqkyb.class);
                List<TJjglSgtYsglssqkyb> filterList = list.stream().filter(item
                        -> !StringUtils.isEmpty(item.getDw()) && !StringUtils.isEmpty(item.getXmmc())).collect(Collectors.toList());
                if(filterList.size() > 0 ){
                    //处理UUID
                    filterList.forEach(item->{
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                    });
                    // 删除相同月份数据
                    // jjglImportMapper.deleteSgtYsglssqkyb(dataTime);
                    int count =  jjglImportMapper.importSgtYsglssqkybData(filterList);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 初设评审月报
     * @param
     */
    public Map<String,Object> importCspsyb(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglCspsyb> list = (List<TJjglCspsyb>)Utils.mapToEntiry(result, TJjglCspsyb.class);
                if(list.size() > 0 ){
                    list=list.stream().collect(Collectors.collectingAndThen(
                            Collectors.toCollection(
                                    ()->new TreeSet<>(Comparator.comparing(TJjglCspsyb::getGcmc))),
                            ArrayList::new));
                    // db存在的项目名称
                    List<String> csPsyYbListDb = jjglImportMapper.getCsPsyYbListDb();
                    // 新增的项目
                    List<TJjglCspsyb> addCollect = list.stream().filter(voItem
                            -> csPsyYbListDb.stream().noneMatch(dbItem -> dbItem.equals(voItem.getGcmc()))).collect(Collectors.toList());
                    // 更新的项目
                    List<TJjglCspsyb> updateCollect = list.stream().filter(voItem
                            -> csPsyYbListDb.stream().anyMatch(dbItem -> dbItem.equals(voItem.getGcmc()))).collect(Collectors.toList());
                    //处理UUID
                    addCollect.forEach(item->{
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                    });
                    if(!CollectionUtils.isEmpty(addCollect)){
                        // 新增
                        int count =  jjglImportMapper.importCspsyb(addCollect);
                        if(count > 0){
                            flag = true;
                        }
                    }
                    // 更新
                    if(!CollectionUtils.isEmpty(updateCollect)){
                        updateCollect.forEach(item->{
                            item.setDataTime(dataTime);
                        });
                        int count = jjglImportMapper.importCspsyb_update(updateCollect);
                        if(count > 0){
                            flag = true;
                        }
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 分部结算附表
     * @param
     */
    public Map<String,Object> importFbjsfb(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglFbjsfb> list = (List<TJjglFbjsfb>)Utils.mapToEntiry(result, TJjglFbjsfb.class);
                if(list.size() > 0 ){
                    //处理UUID
                    list.forEach(item->{
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                    });
                    jjglImportMapper.deleteFbjsfb(dataTime);
                    int count =  jjglImportMapper.importFbjsfb(list);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 结算项目结算概算金额
     * @param
     */
    public Map<String,Object> importJsxmJsgsje(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglJsxmJsgsje> list = (List<TJjglJsxmJsgsje>)Utils.mapToEntiry(result, TJjglJsxmJsgsje.class);
                List<TJjglJsxmJsgsje> collect = list.stream().filter(item ->
                        !StringUtils.isEmpty(item.getXh()) && !StringUtils.isEmpty(item.getXmgcmc())).collect(Collectors.toList());
                Map<String, List<TJjglJsxmJsgsje>> collectByXuHao = list.stream().filter(item -> item.getXh().indexOf(".") == -1).collect(Collectors.groupingBy(TJjglJsxmJsgsje::getXh));
                if(collect.size() > 0 ){
                    //处理UUID
                    collect.forEach(item->{
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                            String[] split = item.getXh().split("\\.");
                            if(split.length > 1){
                                String fuXh = split[0];
                                List<TJjglJsxmJsgsje> tJjglJsxmJsgsjes = collectByXuHao.get(fuXh);
                                if(!CollectionUtils.isEmpty(tJjglJsxmJsgsjes)){
                                    String jsjhwcrqgw = tJjglJsxmJsgsjes.get(0).getJsjhwcrqgw();
                                    item.setJsjhwcrqgw(jsjhwcrqgw);
                                }
                            }
                    });
                    jjglImportMapper.deleteJsxmJsgsje(dataTime);
                    int count =  jjglImportMapper.importJsxmJsgsje(collect);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 投产项目尾工过程管理跟踪表-本期关闭项目
     * @param
     */
    public Map<String,Object> importTcxmWggcglgzbBqgbxm(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglTcxmWggcglgzbBqgbxm> list = (List<TJjglTcxmWggcglgzbBqgbxm>)Utils.mapToEntiry(result, TJjglTcxmWggcglgzbBqgbxm.class);
                List<TJjglTcxmWggcglgzbBqgbxm> collect = list.stream().filter(item ->
                        !StringUtils.isEmpty(item.getJgdw()) && !StringUtils.isEmpty(item.getDydj())
                                && !StringUtils.isEmpty(item.getXmmc())).collect(Collectors.toList());
                if(collect.size() > 0 ){
                    //处理UUID
                    collect.forEach(item->{
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                    });
                    jjglImportMapper.deleteTcxmWggcglgzbBqgbxm(dataTime);
                    int count =  jjglImportMapper.importTcxmWggcglgzbBqgbxm(collect);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 投产项目尾工过程管理跟踪表-投产项目
     * @param
     */
    public Map<String,Object> importTcxmWggcglgzbTcxm(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglTcxmwggcglgzbTcxm> list = (List<TJjglTcxmwggcglgzbTcxm>)Utils.mapToEntiry(result, TJjglTcxmwggcglgzbTcxm.class);
                List<TJjglTcxmwggcglgzbTcxm> collect = list.stream().filter(item ->
                        !StringUtils.isEmpty(item.getJgdw()) && !StringUtils.isEmpty(item.getDydj())
                                && !StringUtils.isEmpty(item.getXmmc())).collect(Collectors.toList());
                if(collect.size() > 0 ){
                    //处理UUID
                    collect.forEach(item->{
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                    });
                    jjglImportMapper.deleteTcxmWggcglgzbTcxm(dataTime);
                    int count =  jjglImportMapper.importTcxmWggcglgzbTcxm(collect);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 工程概算，结算，决算汇总表
     * @param
     */
    public Map<String,Object> importGcgsJsjshzb(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglGcgsJsjshzb> list = (List<TJjglGcgsJsjshzb>)Utils.mapToEntiry(result, TJjglGcgsJsjshzb.class);
                if(list.size() > 0 ){
                    //处理UUID
                    list.forEach(item->{
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                    });
                    jjglImportMapper.deleteGcgsJsjshzb(dataTime);
                    int count =  jjglImportMapper.importGcgsJsjshzb(list);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 技经重点工作月度小结
     * @param
     */
    public Map<String,Object> importJjzdgzydxj(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglJjzdgzydxj> list = (List<TJjglJjzdgzydxj>)Utils.mapToEntiry(result, TJjglJjzdgzydxj.class);
                if(list.size() > 0 ){
                    //处理UUID
                    for (int i = 0; i < list.size(); i++) {
                        TJjglJjzdgzydxj item = list.get(i);
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                        item.setSort(i);
                    }
                    jjglImportMapper.deleteJjzdgzydxj(dataTime);
                    int count =  jjglImportMapper.importJjzdgzydxj(list);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 各单位预算执行情况
     * @param
     */
    public Map<String,Object> importYszxqk(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglYszxqk> list = (List<TJjglYszxqk>)Utils.mapToEntiry(result, TJjglYszxqk.class);
                if(list.size() > 0 ){
                    //处理UUID
                    for (int i = 0; i < list.size(); i++) {
                        TJjglYszxqk item = list.get(i);
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                        item.setSort(i);
                    }
                                 jjglImportMapper.deleteYszxqk(dataTime);
                    int count =  jjglImportMapper.importYszxqk(list);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }


    /**
     * 导入 -------- 尾工项目情况
     * @param
     */
    public Map<String,Object> importWgxmqk(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglWgxmqk> list = (List<TJjglWgxmqk>)Utils.mapToEntiry(result, TJjglWgxmqk.class);
                if(list.size() > 0 ){
                    //处理UUID
                    for (int i = 0; i < list.size(); i++) {
                        TJjglWgxmqk item = list.get(i);
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                        item.setSort(i);
                    }
                                    jjglImportMapper.deleteWgxmqk(dataTime);
                    int count =  jjglImportMapper.importWgxmqk(list);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 农民工工资汇总
     * @param
     */
    public Map<String,Object> importNmggzHz(String propName, MultipartFile[] files,String dataTime){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglNmggzHz> list = (List<TJjglNmggzHz>)Utils.mapToEntiry(result, TJjglNmggzHz.class);
                if(list.size() > 0 ){
                    //处理UUID
                    for (int i = 0; i < list.size(); i++) {
                        TJjglNmggzHz item = list.get(i);
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDataTime(dataTime);
                        item.setSort(i);
                    }
                                    jjglImportMapper.deleteNmggzHz(dataTime);
                    int count =  jjglImportMapper.importNmggzHz(list);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 导入 -------- 年度计划
     * @param
     */
    public Map<String,Object> importExcelNdJh(String propName, MultipartFile[] files,String jsgldw){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<Ndjh> list = (List<Ndjh>)Utils.mapToEntiry(result, Ndjh.class);
                if(list.size() > 0 ){
                    String xmbm = "";
                    String dydj = "";
                    String sd = "";
                    List<Ndjh> listXm = new ArrayList<>();
                    List<Ndjh> listGc = new ArrayList<>();
                    //处理UUID
                    for (int i = 0; i < list.size(); i++) {
                        Ndjh item = list.get(i);
                        boolean flags = true;
                        if(!StringUtils.isEmpty(item.getXmbm())){
                            xmbm = item.getXmbm();
                            flags = false;
                        }
                        if(!StringUtils.isEmpty(item.getDydj())){
                            dydj = item.getDydj();
                        }
                        if(!StringUtils.isEmpty(item.getSd())){
                            sd = item.getSd();
                        }
                        item.setKyps(setDate(item.getKyps()));
                        item.setHz(setDate(item.getHz()));
                        item.setCsps(setDate(item.getCsps()));
                        item.setSgtyssc(setDate(item.getSgtyssc()));
                        item.setSgzb(setDate(item.getSgzb()));
                        item.setKgjs(setDate(item.getKgjs()));
                        item.setTcjs(setDate(item.getTcjs()));
                        item.setSgs("江苏");
                        item.setXmbm(xmbm);
                        item.setDydj(dydj);
                        item.setSd(sd);
                        item.setId(IdUtil.fastSimpleUUID());
                        if(flags){
                            listGc.add(item);
                        }else {
                            listXm.add(item);
                        }
                    }
                    //数据清除当前 建设管理单位对应的数据
                    jjglImportMapper.deleteNdjh(jsgldw);
                    jjglImportMapper.deleteNdjhDxgc(jsgldw);
                    if(!CollectionUtils.isEmpty(listGc)){
                        jjglImportMapper.importExcelNdJh_dxgc(listGc,jsgldw);
                    }
                    if(!CollectionUtils.isEmpty(listXm)){
                        jjglImportMapper.importExcelNdJh(listXm,jsgldw);
                    }
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }


    /**
     * 导入 -------- 年度计划2
     * @param
     */
    public Map<String,Object> importExcelNdJh2(String propName, MultipartFile[] files){
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<NdjhVO2> list = (List<NdjhVO2>)Utils.mapToEntiry(result, NdjhVO2.class);
                if(list.size() > 0 ){
                    int count =  jjglImportMapper.importExcelNdJh2(list);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }


    public static String setDate(String date){
        if(StringUtils.isEmpty(date)){
            return "";
        }
        String[] split = date.split("\\.");
        String out = "";
        for (int i = 0; i < split.length; i++) {
            if(i==1){
                int length = split[i].length();
                if(length == 1){
                    split[1]  = "0"+split[1];
                }
            }
            out = out +"-"+ split[i];
        }
        return (out+"-01").substring(1,(out+"-01").length());
    }
}
